package Solutions


func maxAlternatingSum(nums []int) int64 {
   var stack=make([]int,len(nums)+1)
   var st=0
   stack[0]=0
   var ans=0
   for i:=0;i<len(nums);i++{
       if nums[i]>=stack[st]{
           st++
           stack[st]=nums[i]
       }else{
           ans+=stack[st]-stack[0]
           st=0
           stack[0]=nums[i]
       }
   }
   ans+=(stack[st]-stack[0])
   return int64(ans)
}